<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html>
<html>
<head>
    <title>
        <c:if test="${empty(data.id)}">添加人群</c:if>
        <c:if test="${!empty(data.id)}">编辑人群</c:if>
    </title>
</head>

<body>

<div id="wrapper">
    <div class="gray-bg">
        <div class="row m-t-sm">
            <div class="col-lg-12">
                <ol class="breadcrumb f-fl">
                    <li>
                        <i class="fa fa-location-arrow"></i><a>人群管理</a>
                    </li>
                    <li class="active">
                        <strong>
                            <c:if test="${empty(data.id)}">添加人群</c:if>
                            <c:if test="${!empty(data.id)}">编辑人群</c:if>
                        </strong>
                    </li>
                </ol>
                <span class="navbar-right  m-r m-t-8"><a href="javascript:history.go(-1);" class="btn-primary btn-add "> <i class='fa fa-mail-reply'></i> 返回</a></span>
            </div>
        </div>
        <div class="wrapper wrapper-content animated fadeInRight">
            <div class="row">
                <div class="col-lg-12">
                    <div class="ibox float-e-margins">
                        <div class="ibox-content">
                            <form method="get" data-ajax="${ctx}/admin/crowd/list" action="${ctx}/admin/crowd/save"  class="form-horizontal" id="base_form">
                                <input  type="hidden" name="id" id="id" value="${crowd.id}"/>
                                <input  type="hidden" name="rid" id="rid" value="${data.rid}"/>
                                <input  type="hidden" name="mid" id="mid" value="${mid}"/>
                                <input  type="hidden" name="mianId" id="mianId" value="${data.mianId}"/>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">编号</label>
                                    <div class="col-sm-3"><input type="text" class="form-control" placeholder="编号" value="${crowd.sn}" name="sn" id="sn" required></div>
                                </div>

                                <div class="form-group">
                                    <label class="col-sm-2 control-label">人群名称</label>
                                    <div class="col-sm-3"><input type="text" class="form-control" placeholder="人群名称" value="${crowd.title}" name="title" id="title" required></div>
                                </div>




                                <c:if test="${!empty(crowd.id)}"><div class="form-group">


                                <input type="hidden" class="form-control" id="fjxm" required>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">互斥人群</label>
                                    <div class="col-sm-9">
                                        <table class="table table-striped table-bordered  table-hover m-b-sm" id="item-table">
                                            <theader>
                                                <tr>
                                                    <th>编号</th>
                                                    <th>人群</th>
                                                    <th>操作</th>
                                                </tr>
                                            </theader>
                                        </table>
                                        <a href="javascript:add_item('', '');">添加</a>
                                    </div>
                                </div></c:if>

                                <div class="hr-line-dashed"></div>
                                <div class="form-group">
                                    <div class="col-sm-4 col-sm-offset-2">
                                        <button class="btn btn-primary" type="button" onclick="save()">保 存</button>
                                        <a href="javascript:history.go(-1);" class="btn btn-white ">  取消</a>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>

    </div>
</div>

<script>




    var check_item_ids = [];
    $(document).ready(function() {
        /*var noprices =$("#prices").val();
         if(noprices == '' || noprices == undefined){
         $("#prices").val(0);
         }*/

        var ids = $("#id").val();
        if(ids != '' && ids != undefined){
            $.ajax({
                method: "POST",
                url: "${ctx}/admin/crowd/voitem",
                data: {id: ids},
                dataType: "json",
                async: false,
                success: function(response) {
                    if (response.code == 0) {
                        var data = response.items;
                        for (var i = 0; i < data.length; i ++) {
                            addItem(data[i].id , data[i].hcrowd , data[i].sn);
                        }
                    }
                }
            });
        }

    });



    function add_item() {

        var mianId = $("#id").val();

        parent.layer.open({
            type: 2,
            title: '选择人群',
            shadeClose: true,
            shade: 0.8,
            area: ['600px', '350px'],
            content: '${ctx}/admin/crowd/additem?id='+ mianId
        });
    }

    function addItem(id,itemTitle, itemStatus) {
        var model = $(
                "<tr>" +
                "<td>" + itemStatus + "</td>" +
                "<td>" + itemTitle + "</td>"
        );
        model.append("<td><a onclick='delItem(this," + id + ");'>删除</a></td></tr>");
        $("#item-table").append(model);
        check_item_ids.push(id);


        $("#fjxm").val(check_item_ids.toString());
        console.log("add:"+check_item_ids.toString())

        return true;
    }

    function delItem(self, id) {
        var i = check_item_ids.indexOf(id);
        check_item_ids.splice(i, 1);
        $(self).parent().parent().remove();

        $.ajax({
            method: "POST",
            url: "${ctx}/admin/crowd/delmx",
            data: {
                id:id
            },
            dataType: "json",
            async: false,
            success: function(response) {
                if (response.code == 0) {
                    $(self).parent().parent().remove();
                }
            }
        });

        $("#fjxm").val(check_item_ids.toString());
        console.log("remove:"+check_item_ids.toString())

    }

    function save(){
       $("#base_form").submit();
    }





</script>
</body>

</html>
